package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Building;
import com.woniuxy.oldmanapi.entity.Housetype;
import com.woniuxy.oldmanapi.service.HousetypeService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HousetypeController {

    @Autowired
    HousetypeService housetypeService;

    @GetMapping("/housetype/list")
    public ResponseResult<PageInfo<Housetype>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name) {
        ResponseResult<PageInfo<Housetype>> result = new ResponseResult<>();
        try {
            PageInfo<Housetype> lists = housetypeService.findHousetypeByPage(now, size, name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @GetMapping("/housetype/all")
    public ResponseResult<List<Housetype>> list() {
        ResponseResult<List<Housetype>> result = new ResponseResult<>();
        try {
            List<Housetype> lists = housetypeService.findHouseTypes();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PostMapping("/housetype/add")
    public ResponseResult<Void> add(@RequestBody Housetype housetype) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            Integer id = housetypeService.findHouseTypeByName(housetype.getHousetype());
            if (id == null) {
                housetypeService.add(housetype);
                result.setCode(200);
                result.setMsg("添加成功!");
            }else{
                result.setCode(202);
                result.setMsg("名称重复，换个名字吧!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/housetype/modify")
    public ResponseResult<Void> modify(@RequestBody Housetype housetype) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            housetypeService.modify(housetype);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/housetype/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            housetypeService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/housetype/one/{id}")
    public ResponseResult<Housetype> one(@PathVariable("id") int id) {
        ResponseResult<Housetype> result = new ResponseResult<>();
        try {
            Housetype housetype = housetypeService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(housetype);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
